package com.rico.deploy.controller;

import com.github.pagehelper.PageInfo;
import com.rico.common.response.MessageUtils;
import com.rico.common.response.RestResponse;
import com.rico.deploy.entity.Course;
import com.rico.deploy.service.CourseService;
import com.rico.deploy.service.SubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 夏智力
 * @FileName CourseController
 * @date 2021/5/13 20:08
 * @Software: IntelliJ IDEA
 */
@Controller
@Slf4j
public class CoursesController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private SubjectService subjectService;

    /**
     * 默认获取第一页学科列表
     * @return
     */
    @GetMapping("course/list")
    @ResponseBody
    public RestResponse getlist(Integer page, Integer limit) {
        log.info("{} {}", page, limit );
        List<Course> courses = courseService.selectAll(page, limit);
        PageInfo<Course> coursePageInfo = new PageInfo(courses);
        return MessageUtils.success(coursePageInfo.getList(), (int) coursePageInfo.getTotal());
    }

    @GetMapping("course/lists")
    @ResponseBody
    public RestResponse getlist(Integer page, Integer limit,@RequestParam(value="keyword",defaultValue = "计算机科学与技术") String keyword) {
        log.info("{} {} {}", page, limit, keyword );
        List<Course> courses = courseService.selectAll(page, limit ,keyword);
        PageInfo<Course> coursePageInfo = new PageInfo(courses);
        return MessageUtils.success(coursePageInfo.getList(), (int) coursePageInfo.getTotal());
    }


    @GetMapping("course/delete")
    @ResponseBody
    public RestResponse deleteById(Integer id) {
        log.info("{}", id);
        String msg = null;
        if ( courseService.deleteCourseById(id) == 1 )
            msg = "删除成功";
        else
            msg = "删除失败";
        return MessageUtils.success(msg);
    }

    /**
     * 更新数据
     * @param course
     * @return
     */
    @PostMapping("course/put")
    @ResponseBody
    public RestResponse put(Course course) {
        int i = courseService.updateCourse(course);
        if ( i == 0 )
            return MessageUtils.fail("编辑失败");

        else
            return MessageUtils.success("编辑成功");
    }

    /**
     * 添加数据
     * @param course
     * @return
     */
    @PostMapping("course/post")
    @ResponseBody
    public RestResponse post(Course course) {
        log.info("{}", course.toString());
        int i = courseService.insertCourse(course);
        if ( i == 0 )
            return MessageUtils.fail("添加失败");

        else
            return MessageUtils.success("添加成功");
    }

    /**
     * 学院左边树
     * @return
     */
    @GetMapping("course/leftMenu")
    @ResponseBody
    public RestResponse LeftTreeView(){
        return MessageUtils.success("操作成功",subjectService.LeftTreeView());
    }
    /**
     * 获取html页面
     * @return
     */
    @GetMapping("courseManage")
    public String gethtml(){
        return "page/manage/courseManage/courseManage";
    }
}
